package com.dynamictpm.core;

import com.dynamictpm.config.DynamicThreadPoolProperties;
import com.dynamictpm.listener.Listener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.Executor;

/**
 * 描述：： 适配器操作类
 * <p>
 * ------------------------------------
 *
 * @program: dynamic-tpm
 * @ClassName ThreadPoolOperation
 * @author: it-yml
 * @create: 2023-07-05 09:53
 * @Version 1.0
 * --------------------------------------
 **/
public class ThreadPoolOperation {
    @Autowired
    private ConfigService configService;

    private final DynamicThreadPoolProperties properties;

    public ThreadPoolOperation(DynamicThreadPoolProperties properties) {
        this.properties = properties;
    }


    public Listener subscribeConfig(String tpId, Executor executor, ThreadPoolSubscribeCallback threadPoolSubscribeCallback) {
        Listener configListener = new Listener() {
            @Override
            public void receiveConfigInfo(String config) {
                threadPoolSubscribeCallback.callback(config);
            }

            @Override
            public Executor getExecutor() {
                return executor;
            }
        };

        configService.addListener(properties.getNamespace(), properties.getItemId(), tpId, configListener);

        return configListener;
    }
}
